package Scene6;

import Picture.GameImage;
import Scnen3.H2O;
import Scnen3.O2;
import Scnen3.Scene3;
import Scnen3.glucose;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TreePanel extends JPanel {

    Scene3 scene3 = new Scene3();
    // TODO delay=1000
    Timer timer = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            now--;
        }
    });
    public int now = 60;


    Bucket bucket = new Bucket();
    Shovel shovel = new Shovel();
    Fertilizer fertilize = new Fertilizer();

    Tree1 tree1 = new Tree1(fertilize, shovel, bucket, this);
    Tree2 tree2 = new Tree2(fertilize, shovel, bucket, this);
    Tree3 tree3 = new Tree3(fertilize, shovel, bucket, this);


    boolean Onpicbucket = false;
    boolean Onpicshovel = false;
    boolean Onpicfertilize = false;
    boolean Canusebucket = true;
    boolean Canuseshovel = true;
    boolean Canusefertilizer = true;
    public  int again = 0;


    int begin_xbucket;
    int begin_ybucket;


    int begin_xshovel;
    int begin_yshovel;


    int begin_xfertilize;
    int begin_yfertilize;
    public int begin = 0;
    public int lose = 0;


    public void reSize(Frame frame) {
        frame.setSize(1560, 875);
        validate();
        repaint();
    }

    Scene6 scene6 = new Scene6();

    public TreePanel(JButton button,JButton button2) {
        //TODO
        this.add(button);
        new Thread(() -> {

            while (true) {
                if(again==1){
                    again = 0;
                    tree1.reOpen();
                    tree2.reOpen();
                    tree3.reOpen();
                    begin = 0;
                    lose = 0;
                    now = 60;
                }
                if(lose==1){
                    this.add(button2);
                }
                if(now<0&&!(tree1.experience==5&&tree2.experience==5&&tree3.experience==5)){
                    lose = 1;
                }
                if(tree1.experience==5&&tree2.experience==5&&tree3.experience==5){
                    this.add(button);
                }
                if (begin == 1) {
                    timer.start();
                }
                tree1.changeState();
                tree1.changeWant();
                tree1.absorb();
                tree2.changeState();
                tree2.changeWant();
                tree2.absorb();
                tree3.changeState();
                tree3.changeWant();
                tree3.absorb();
                repaint();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }).start();


        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {


                if (isOnPicbucket(e.getX(), e.getY())) {
                    begin_xbucket = e.getX();
                    begin_ybucket = e.getY();
                    Onpicbucket = true;
                    repaint();
                }

                if (isOnPicshovel(e.getX(), e.getY())) {
                    begin_xshovel = e.getX();
                    begin_yshovel = e.getY();
                    Onpicshovel = true;
                    repaint();
                }

                if (isOnPicfertilize(e.getX(), e.getY())) {
                    begin_xfertilize = e.getX();
                    begin_yfertilize = e.getY();
                    Onpicfertilize = true;
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                Onpicbucket = false;
                Onpicshovel = false;
                Onpicfertilize = false;
            }

        });

        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (Onpicbucket && check(e.getX(), e.getY()) && Canusebucket) {
                    bucket.x = bucket.x - (begin_xbucket - e.getX());
                    bucket.y = bucket.y - (begin_ybucket - e.getY());
                    begin_xbucket = e.getX();
                    begin_ybucket = e.getY();
                    repaint();
                }

                if (Onpicshovel && check(e.getX(), e.getY()) && Canuseshovel) {
                    shovel.x = shovel.x - (begin_xshovel - e.getX());
                    shovel.y = shovel.y - (begin_yshovel - e.getY());
                    begin_xshovel = e.getX();
                    begin_yshovel = e.getY();
                    repaint();

                }

                if (Onpicfertilize && check(e.getX(), e.getY()) && Canusefertilizer) {
                    fertilize.x = fertilize.x - (begin_xfertilize - e.getX());
                    fertilize.y = fertilize.y - (begin_yfertilize - e.getY());
                    begin_xfertilize = e.getX();
                    begin_yfertilize = e.getY();
                    repaint();
                }
            }
        });
    }

    private boolean isOnPicbucket(int x, int y) {
        if (x >= bucket.x && x <= bucket.x + bucket.img.getWidth() && y >= bucket.y && y <= bucket.y + bucket.img.getHeight()) {
            return true;
        }
        return false;
    }

    private boolean isOnPicshovel(int x, int y) {
        if (x >= shovel.x && x <= shovel.x + shovel.img.getWidth() && y >= shovel.y && y <= shovel.y + shovel.img.getHeight()) {
            return true;
        }
        return false;
    }

    private boolean isOnPicfertilize(int x, int y) {
        if (x >= fertilize.x && x <= fertilize.x + fertilize.img.getWidth() && y >= fertilize.y && y <= fertilize.y + fertilize.img.getHeight()) {
            return true;
        }
        return false;
    }

    private boolean check(int x, int y) {
        if (x < 0 || y < 0) {
            return false;

        }
        if (x > getWidth() || y > getHeight()) {
            return false;
        }
        return true;
    }


    public void paint(Graphics g) {
        super.paint(g);
        if(lose==0) {

            if (tree1.experience == 5 && tree2.experience == 5 && tree3.experience == 5) {
                g.drawImage(GameImage.get("/Picture/背景1.png"), 0, 0, null);
                g.drawImage(tree1.img, tree1.x, tree1.y, null);
                g.drawImage(tree2.img, tree2.x, tree2.y, null);
                g.drawImage(tree3.img, tree3.x, tree3.y, null);
                g.drawImage(GameImage.get("/Picture/win2.png"), 400, 300, null);
                g.drawImage(GameImage.get("/Picture/Scene3/button.png"), 650, 700, null);
                g.drawImage(GameImage.get("/Picture/数字/好耶.png"), 680, 720, null);
            } else {

                g.drawImage(scene6.map.get("bg"), 0, 0, null);

                g.drawImage(tree1.img, tree1.x, tree1.y, null);
                g.drawImage(tree2.img, tree2.x, tree2.y, null);
                g.drawImage(tree3.img, tree3.x, tree3.y, null);

                g.drawImage(bucket.img, bucket.x, bucket.y, null);
                g.drawImage(fertilize.img, fertilize.x, fertilize.y, null);
                g.drawImage(shovel.img, shovel.x, shovel.y, null);
                g.drawImage(scene6.map.get("铲子"), 50, 300, null);
                g.drawImage(scene6.map.get("肥料"), 50, 580, null);
                g.drawImage(scene6.map.get("水桶"), 50, 40, null);
                g.drawImage(scene6.map.get("道具框"), 0, 0, null);
                g.drawImage(scene6.map.get("道具框"), 0, 250, null);
                g.drawImage(scene6.map.get("道具框"), 0, 520, null);
                if (tree1.want == 1) {
                    Canusebucket = true;
                    g.drawImage(scene6.map.get("对话框"), 430, 420, null);
                    g.drawImage(scene6.map.get("水桶s"), 470, 430, null);
                }
                if (tree1.want == 2) {
                    Canuseshovel = true;
                    g.drawImage(scene6.map.get("对话框"), 430, 420, null);
                    g.drawImage(scene6.map.get("铲子s"), 470, 430, null);
                }
                if (tree1.want == 3) {
                    Canusefertilizer = true;
                    g.drawImage(scene6.map.get("对话框"), 430, 420, null);
                    g.drawImage(scene6.map.get("肥料s"), 470, 430, null);
                }
                if (tree2.want == 1) {
                    Canusebucket = true;
                    g.drawImage(scene6.map.get("对话框"), 850, 420, null);
                    g.drawImage(scene6.map.get("水桶s"), 900, 430, null);
                }
                if (tree2.want == 2) {
                    Canuseshovel = true;
                    g.drawImage(scene6.map.get("对话框"), 850, 420, null);
                    g.drawImage(scene6.map.get("铲子s"), 900, 430, null);
                }
                if (tree2.want == 3) {
                    Canusefertilizer = true;
                    g.drawImage(scene6.map.get("对话框"), 850, 420, null);
                    g.drawImage(scene6.map.get("肥料s"), 900, 430, null);
                }
                if (tree3.want == 1) {
                    Canusebucket = true;
                    g.drawImage(scene6.map.get("对话框"), 1300, 420, null);
                    g.drawImage(scene6.map.get("水桶s"), 1350, 430, null);
                }
                if (tree3.want == 2) {
                    Canuseshovel = true;
                    g.drawImage(scene6.map.get("对话框"), 1300, 420, null);
                    g.drawImage(scene6.map.get("铲子s"), 1350, 430, null);
                }
                if (tree3.want == 3) {
                    Canusefertilizer = true;
                    g.drawImage(scene6.map.get("对话框"), 1300, 420, null);
                    g.drawImage(scene6.map.get("肥料s"), 1350, 430, null);
                }
                g.drawImage(GameImage.get("/Picture/images/计时器3(1).png"), 900, 100, null);

                Font font = new Font("Calibri", Font.BOLD, 40);
                g.setColor(Color.red);
                g.setFont(font);
                g.drawString("" + now, 1050, 140);
            }

        }
        else{

            g.drawImage(scene6.map.get("bg"), 0, 0, null);
            g.drawImage(GameImage.get("/Picture/Scene3/button.png"), 650, 650, null);
            g.drawImage(GameImage.get("/Picture/数字/再来一遍.png"), 660, 660, null);
            g.drawImage(GameImage.get("/Picture/lose(2).png"),300,300,null);
        }


    }
}
